﻿Imports System.Collections.Generic

Partial Class WebForm_SearchUserControl
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Visible = False

        If Not IsPostBack Then
            Me.BindDDLOrganization()
            Me.BindDDLUserType()
        End If
    End Sub

    Protected Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
        RaiseEvent Saving(Me, New EventArgs)
    End Sub

    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Me.BindGV()
        Me.Show()
    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
        Me.Show()
    End Sub
End Class

Partial Class WebForm_SearchUserControl
    Private Sub BindDDLOrganization()
        Me.ddlOrganization.DataTextField = "OrganizationName"
        Me.ddlOrganization.DataValueField = "UOrganizationID"
        Me.ddlOrganization.DataSource = MyObj.GetDDLOrganizationData()
        Me.ddlOrganization.DataBind()

    End Sub

    Private Sub BindDDLUserType()
        Me.ddlUserType.DataTextField = "Text"
        Me.ddlUserType.DataValueField = "Value"
        Me.ddlUserType.DataSource = MyObj.GetCodeMappingData("User", "UserType")
        Me.ddlUserType.DataBind()

        Me.ddlUserType.Items.Insert(0, MyObj.GetDDLSelectAllItem())

    End Sub

    Public Sub Show()
        Me.mpSearch.Show()
        Me.Visible = True
    End Sub

    Public Sub ShowByRoleID(ByVal roleID As String)
        Me.RoleID = roleID
        Me.BindGVByRoleID()

        Me.mpSearch.Show()
        Me.Visible = True
    End Sub

    Private Sub BindGV()
        If Me.RoleID <> "" Then
            Me.BindGVByRoleID()            
        End If
    End Sub

    Private Sub BindGVByRoleID()
        Dim dat As New clsData
        Dim sql As New MyUserDataFilter

        sql.Length = 0
        sql.AppendFormat("select u.* ")
        sql.AppendFormat("from GetUserByURoot('{0}') u ", Me.ddlOrganization.SelectedValue)
        sql.AppendFormat("left join RoleUserMapping ru on u.UserID = ru.UserID and ru.RoleID = '{0}' ", Me.RoleID)
        sql.AppendFormat("where u.Active = 'Y' ")
        sql.AppendFormat("and ru.RoleID is null ")
        'sql.AppendFormat("and ro.RoleID in ({0}) and ro.AuthName in ('Add', 'Admin') ", clsAuth.GetRoleList())

        sql.AppendFormat(Me.GetWhereSQL())

        Me.gv.DataSource = dat.GetData(sql)
        Me.gv.DataBind()

    End Sub

    Private Function GetWhereSQL() As String
        Dim sql As New MyStringBuilder

        If Me.ddlUserType.SelectedValue <> "" Then
            sql.AppendFormat("and u.UserType = '{0}' ", Me.ddlUserType.SelectedValue)
        End If

        If Me.txtUserID.Text <> "" Then
            sql.AppendFormat("and u.UserID like '%{0}%' ", Me.txtUserID.Text)
        End If

        If Me.txtUserName.Text <> "" Then
            sql.AppendFormat("and u.UserName like '%{0}%' ", Me.txtUserName.Text)
        End If

        Return sql.ToString()
    End Function

    Public ReadOnly Property SelectedUserList() As List(Of String)
        Get
            Dim userList As New List(Of String)

            For Each row As GridViewRow In Me.gv.Rows
                Dim key As DataKey = Me.gv.DataKeys(row.RowIndex)

                If TryCast(row.FindControl("chkUserID"), CheckBox).Checked = True Then
                    userList.Add(key("UserID"))
                End If
            Next

            Return userList
        End Get
    End Property

    Public Event Saving(ByVal sender As Object, ByVal e As System.EventArgs)

    Public Property RoleID() As String
        Get
            Return ViewState("RoleID")
        End Get
        Set(ByVal value As String)
            ViewState("RoleID") = value
        End Set
    End Property
End Class